﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using System.Collections;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using ProgNetComponentsDx.ChangeDetect;

namespace ProgNetComponentsDx.Design
{
    public class DxGridColumnDesigner : ComponentDesigner
    {
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection list = new DesignerVerbCollection();
                list.Add(new DesignerVerb("GridColumn Repositories", new EventHandler(GetRepositories)));
                return list;
            }
        }
        private void GetRepositories(object sender, EventArgs e)
        {
            List<GridColumn> columns = new List<GridColumn>();
            if (this.Component!=null)
            {
                IDictionary AllObjects = this.Component.GetFieldValue("AllObjects") as IDictionary;

                if (AllObjects != null)
                {
                    foreach (object obj in AllObjects.Keys)
                    {
                        if (obj is ColumnView)
                        {
                            foreach (GridColumn col in (obj as ColumnView).Columns)
                            {
                                if (!columns.Contains(col))
                                    columns.Add(col);
                            }
                        }
                        else if (obj is GridColumn)
                        {
                            if ((obj as GridColumn).View != null)
                            {
                                if (!columns.Contains((obj as GridColumn)))
                                    columns.Add((obj as GridColumn));
                            }
                        }
                    }
                }
            }
            try
            {
                ColumnRepositoryCreator creator = new ColumnRepositoryCreator(columns);
                IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
                creator.Host = host;
                creator.ShowDialog();
            }
            catch 
            {
               
            }
        }
    }
}
